<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@taglib uri="/WEB-INF/page-base.tld" prefix="w" %>
<%String path = request.getContextPath(); %>
<w:define bind="#moneyOff_classid" id="classid1"/>
<script>
    function loadclasstreeok() {
        $("#classtree1").combotree("setValue", "<%=classid1%>");
    }
</script>
<style>
    .btn {
        text-decoration: none;
        background-color: #00A4F2;
        color: #fff;
        padding: 5px 20px;
        border-radius: 5px;
    }

    .btn:hover {
        background-color: #00ADFF;
    }

    .ui-btn {
        padding: 15px;
    }

    .newbtna {
        color: red;
        text-decoration: none;
        margin-left: 2px;
    }

    .inpradio {
        margin-top: 0;
    }

    .datainp {
        width: 173px;
        height: 30px !important;
        border: 1px #A5D2EC solid;
    }

    .wicon {
        background-image: url(
        "");
        background-repeat: no-repeat;
        background-position: right center;
    }

    #errormsg {
        position: fixed;
        width: 200px;
        height: 50px;
        line-height: 50px;
        padding: 1px 5px;
        text-align: center;
        background-color: #00A4F2;
        opacity: 0.5;
        color: #fff;
        left: 50%;
        margin-left: -50px;
        top: 50%;
        border-radius: 20px;
    }
</style>
<form id="cutform" method="post" action="/marketing/FillReductionPage@chooseMoneyList.page">
    <div id="errormsg" style="display: none;"></div>
    <div class="headT">
        <span></span>
        <label>活动定义</label>
    </div>
    <div class="ui-formContent">
        <p>
            <w:hidden bind="bean.id" name="moneyid"/>
            <w:hidden bind="bean.promotid" name="promotionid"/>
            <w:hidden bind="bean.start_time"/>
            <w:hidden bind="bean.end_time"/>
            <w:define id="bean_items" bind="bean.items"/>
            <w:define id="bean_itemsrate" bind="bean.itemsrate"/>
            <label style="width:130px;text-align:right">推广平台：</label>
            <w:radio name="platform" bind="bean.platform" value="0" text="全平台"/>
            <w:radio name="platform" bind="bean.platform" value="1" text="PC端"/>
            <w:radio name="platform" bind="bean.platform" value="2" text="微商城"/>
            <font class="red">*</font>
            <label style="width:130px;text-align:right">活动时间：</label>
            <input class="datainp wicon" id="singEditInpstart" name="starttime" value="" type="text" placeholder="请选择"
                   readonly>到
            <input class="datainp wicon" id="singEditinpend" name="endtime" value="" type="text" placeholder="请选择"
                   readonly>
            <font class="red">*</font>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动标题：</label>
            <input value="<w:write bind="bean.name" />" name="promotname" maxlength="45" style="width:742px"
                   styleClass="easyui-validatebox" autocomplete="off"/>
            <font class="red">*</font>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动描述：</label>
            <input value="<w:write bind="bean.description" />" name="description" style="width:742px;" maxlength="30"
                   styleClass="easyui-validatebox" autocomplete="off"/>
        </p>
        <p>
            <label style="width:130px;text-align:right">排序：</label>
            <input value="<w:write bind="bean.sort_value" />" name="sort_value" maxlength="8"
                   onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                   style="width:100px" styleClass="easyui-validatebox" autocomplete="off"/>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动专区描述：</label>
            <input value="<w:write bind="bean.district_description"/>" name="district_description" style="width:731px"
                   maxlength="16" styleClass="easyui-validatebox" autocomplete="off"/>
        </p>
    </div>
    <div class="headT">
        <span></span>
        <label>规则限制</label>
    </div>
    <div class="ui-formContent">
        <p id="select">
            <label style="width:130px;text-align:right">活动规则：</label>
            <label class="radioButton"><input id="mj" name="bean2" class="inpradio" type="radio"
                                              value="0"/>满减</label>
            <label class="radioButton"><input id="mz" name="bean2" type="radio" class="inpradio" value="1"/>满折</label>
            <font class="red">*</font>
        </p>
        <input type="hidden" name="manjian" value="<w:write bind="bean.items"/>">
        <div id="pro1">
            <w:define id="bean_items" bind="bean.items"/>
            <w:switch>
                <w:case bind="#bean_items.size <= 0">
                    <p style="margin-left: 153px;display: none;" class="cut" id="cut" name="money_tag">
                        满<input type="text" name="off_money" autocomplete="off" maxlength="8"
                                value="<w:write bind="#info.off_money"/>"
                                style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                        减<input type="text" name="discount_money" autocomplete="off" maxlength="8"
                                value="<w:write bind="#info.discount_money"/>"
                                style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                        <a id="cut_add" href="#" style="color: red;text-decoration: none;">[+] </a>

                    </p>
                </w:case>
                <w:case bind="#bean_items.size >= 1">
                    <w:iterate id="info" bind="bean.items" indexId="info_index">
                        <w:define id="info_discount_money" bind="#info.discount_money"/>
                        <w:define id="info_discount_rate" bind="#info.discount_rate"/>
                        <p style="margin-left: 153px;display: none;" rmenu="<w:write bind="#info.id"/>" class="cut"
                           id="cut" name="money_tag">
                            <w:switch>
                                <w:case bind="#info_index==0">
                                    <input type="hidden" name="<w:write bind="#info.id"/>"
                                           value="<w:write bind="#info.id"/>">
                                    满<input type="text" name="off_money" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#info.off_money"/>"
                                    style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    减<input type="text" name="discount_money" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#info.discount_money"/>"
                                    style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    <a id="cut_add" href="#" style="color: red;text-decoration: none;">[+] </a>
                                </w:case>
                                <w:else>
                                    <input type="hidden" name="offid" value="<w:write bind="#info.id"/>">
                                    满<input type="text" name="off_money" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#info.off_money"/>"
                                    style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    减<input type="text" name="discount_money" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#info.discount_money"/>"
                                    style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    <a href='javascript:;' class='newbtna' onclick='del(this)'> [-]</a>
                                </w:else>
                            </w:switch>
                        </p>
                    </w:iterate>
                </w:case>
            </w:switch>
        </div>
        <div id="pro2">
            <w:define id="bean_itemsrate" bind="bean.itemsrate"/>
            <w:switch>
                <w:case bind="#bean_itemsrate.size <= 0">
                    <p style="margin-left: 153px;display: none;" name="money_tag_rate" class="folding" id="folding">
                        满<input type="text" name="off_money_rat" autocomplete="off" maxlength="8"
                                value="<w:write bind="#item.off_money"/>"
                                style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                        折扣<input type="text" name="discount_rate" autocomplete="off"
                                 value="<w:write bind="#item.discount_rate"/>"
                                 maxlength="2" style="width:100px;margin:0 5px;" oninput="verify_rate(this);"
                                 onblur="verify_rate_to(this);">%
                        <a id="folding_add" href="#" style="color: red;text-decoration: none;">[+]</a>
                        <em style="color: #888888;font-style: normal;">( 如需要打八折,请输入80 )</em>
                    </p>
                </w:case>
                <w:case bind="#bean_itemsrate.size >= 1">
                    <w:iterate id="item" bind="bean.itemsrate" indexId="item_index">
                        <w:define id="item_discount_money" bind="#item.discount_money"/>
                        <w:define id="item_discount_rate" bind="#item.discount_rate"/>
                        <input type="hidden" name="offid_rate" value="<w:write bind="#item.id"/>">
                        <p style="margin-left: 153px;display: none;" name="money_tag_rate"
                           rmenu="<w:write bind="#item.id"/>" class="folding" id="folding<w:write bind="#item_index"/>">
                            <w:switch>
                                <w:case bind="#item_index==0">
                                    满<input type="text" name="off_money_rat" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#item.off_money"/>" style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    折扣<input type="text" name="discount_rate" autocomplete="off" value="<w:write
                                        bind="#item.discount_rate"/>" style="width:100px;margin:0 5px;" placeholder="请输整数" maxlength="2" oninput="verify_rate(this);" onblur="verify_rate_to(this);">%
                                    <a id="folding_add" href="#" style="color: red;text-decoration: none;">[+]</a>
                                    <em style="color: #888888;font-style: normal;">( 如需要打八折,请输入80 )</em>
                                </w:case>
                                <w:else>
                                    满<input type="text" name="off_money_rat" maxlength="8" autocomplete="off" value="<w:write
                                        bind="#item.off_money"/>" style="width:100px;margin:0 5px;" onblur="inputcheck(this);">
                                    折扣<input type="text" name="discount_rate" autocomplete="off" value="<w:write
                                        bind="#item.discount_rate"/>" placeholder="请输整数" maxlength="2" oninput="verify_rate(this);" onblur="verify_rate_to(this);" style="width:100px;margin:0 5px;">%
                                    <a href='javascript:;' class='newbtna' onclick='del(this)'> [-]</a>

                                </w:else>
                            </w:switch>
                        </p>
                    </w:iterate>
                </w:case>
            </w:switch>


        </div>

        <div style="border-top: 1px solid #D1D0EA;"></div>
        <p>
            </font>
            <label style="width:130px;text-align:right">参与次数：</label>
            <input value="<w:write bind="bean.time" />" name="time" style="width:100px" styleClass="easyui-validatebox"
                   autocomplete="off" onkeyup="this.value=this.value.replace(/[^\d]/g,'')"/>次
        </p>
        <div style="border-top: 1px solid #D1D0EA;"></div>
        <p>
            <label style="width:130px;text-align:right;float: left;">客户范围：</label>
            <select name="controlsales" id="controlsales"
                    style="width:386px;height:200px;border:1px solid #DDDDDD;margin-left: 9px;" multiple="multiple">
                <w:iterate id="info" bind="#controlsales">
                    <option value="{'type':'<w:write bind="#info.type"/>','val':'<w:write bind="#info.val"/>','text':'<w:write bind="#info.text"/>'}">
                        <w:write bind="#info.text"/></option>
                </w:iterate>
            </select>
        </p>
        <div style="margin-top:8px;margin-left: 149px;">
            <a href="javascript:void(0);" class="ui_btn marginR8" onclick="addControlsales()">新增控销</a>
            <a href="javascript:void(0);" class="ui_btn marginR8" onclick="delControlsales()">删除控销</a>
        </div>
    </div>
    <div class="headT">
        <span></span>
        <label>参与方式</label>
    </div>
    <div class="ui-formContent" id="pro_class_set">
        <p style="margin-left: 55px;">
            <input type="hidden" value="<w:write bind="bean.product_type"/>" name="product_type_val">
            <label class="radiocolor"><input name="bean4" id="qc" type="radio" class="inpradio" value="0">全场商品</label>
            <label class="radiocolor"><input name="bean4" id="bf" type="radio" class="inpradio" value="2">部分商品参加</label>
            <label class="radiocolor"><input name="bean4" id="zd" type="radio" class="inpradio" value="3">指定类目</label>
        </p>

        <p id="puctfl">
            <label title="商品分类" style="width:130px;text-align:right">商品分类：</label>
            <select id="classtree1" name="bean.classid1" class="easyui-combotree" style="width:250px;height:28px;"
                    data-options="url:'<%=path %>/products/ProductClassPage@loadClassTree2.page',onLoadSuccess:loadclasstreeok"></select>
        </p>
    </div>

    <div style="width: 100%;  text-align: center; position: absolute;z-index: 10000;bottom:0;"
         class="ui-buttonBar ui-btn">
        <input type="submit" value="完成"/>
    </div>
    <div class="easyui-panel ui-toolBar" border="false" id="selectedit" data-options="doSize:false">
        <table cellpadding="0" cellspacing="0" style="margin-top:5px">
            <tr>
                <td width="70" align="right">商品名称：</td>
                <td><w:text bind="bean.pname" style="width:120px;height:22px;"/></td>
                <td width="70" align="right">商品分类：</td>
                <td><select id="classtree" name="bean.classid" class="easyui-combotree"
                            style="width:250px;height:28px;"
                            data-options="url:'<%=path %>/products/ProductClassPage@loadClassTree2.page'"></select>
                </td>


                <td width="70" align="right">商品编码：</td>
                <td><w:text bind="bean.code" style="width:300px;height:22px;"
                            onchange="value=(value.match(/\d+/g)||['']).join(',')"/></td>
                <td>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" iconCls="icon_search"
                             onclick="query1()">查询</a>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" iconCls="icon_search"
                             onclick="resetform()">重置</a>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" iconCls="icon_search" onclick="inp(this);"
                             id="see">查看已添加商品</a>
                </td>
            </tr>
        </table>
    </div>
</form>

<div title="部分商品" style="border: 1px solid #DDDDDD;">
    <script>
        window.productItems = {};
        window.productItemsTo = {};
        window.primarys = new Array();
        $(function () {
            <w:switch>
            <w:case bind="#productitems_to !=null">
            <w:iterate id="items_pro" bind="#productitems_to" indexId="index">

            var tempProductItem<w:write bind="#index"/> = {};
            var tempProductItemTo<w:write bind="#index"/> = {};
            //获取商品属性
            tempProductItemTo<w:write bind="#index"/>.showlistidto = "<w:write bind="#items_pro.id"/>";
            tempProductItem<w:write bind="#index"/>.showlistid = "<w:write bind="#items_pro.id"/>";
            tempProductItem<w:write bind="#index"/>.showlistname = "<w:write bind="#items_pro.pname"/>";
            tempProductItem<w:write bind="#index"/>.price = "<w:write bind="#items_pro.price"/>";
            tempProductItem<w:write bind="#index"/>.showlistcode = "<w:write bind="#items_pro.code"/>";
            tempProductItem<w:write bind="#index"/>.showlistB_chanjia = "<w:write bind="#items_pro.B_chanjia"/>";
            tempProductItem<w:write bind="#index"/>.showlistB_guige = "<w:write bind="#items_pro.B_guige"/>";
            tempProductItem<w:write bind="#index"/>.showlistclassname = "<w:write bind="#items_pro.classname"/>";
            tempProductItem<w:write bind="#index"/>.showlistproductamount = "<w:write bind="#items_pro.productamount"/>";
            tempProductItem<w:write bind="#index"/>.showlisttypes = "<w:write bind="#items_pro.moneyofftype"/>";
            productItems["key" + tempProductItem<w:write bind="#index"/>.showlistid] = tempProductItem<w:write bind="#index"/>;
            productItemsTo["key" + tempProductItemTo<w:write bind="#index"/>.showlistidto] = tempProductItemTo<w:write bind="#index"/>;
            </w:iterate>
            </w:case>
            <w:case bind="#singleProducitem != null">

            var tempProductItem = {};
            var tempProductItemTo = {};
            //获取商品属性
            tempProductItemTo.showlistidto = "<w:write bind="#singleProducitem.id"/>";
            tempProductItem.showlistid = "<w:write bind="#singleProducitem.id"/>";
            tempProductItem.price = "<w:write bind="#singleProducitem.price"/>";
            tempProductItem.showlistname = "<w:write bind="#singleProducitem.pname"/>";
            tempProductItem.showlistcode = "<w:write bind="#singleProducitem.code"/>";
            tempProductItem.showlistB_chanjia = "<w:write bind="#singleProducitem.B_chanjia"/>";
            tempProductItem.showlistB_guige = "<w:write bind="#singleProducitem.B_guige"/>";
            tempProductItem.showlistclassname = "<w:write bind="#singleProducitem.classname"/>";
            tempProductItem.showlistproductamount = "<w:write bind="#singleProducitem.productamount"/>";
            tempProductItem.showlisttypes = "<w:write bind="#singleProducitem.moneyofftype"/>";
            productItems["key" + tempProductItem.showlistid] = tempProductItem;
            productItemsTo["key" + tempProductItemTo.showlistidto] = tempProductItemTo;
            </w:case>
            </w:switch>
        });
    </script>

    <div style="border: 1px solid #DDDDDD;">
        <div id="chooseShowListPanel" class="easyui-panel" border="false" data-options="doSize:false,href:''">

        </div>
    </div>
</div>

<div style="width: 100%;height: 100px;"></div>


<script>
    $(function () {
        $("a.panel-tool-close").unbind("click");
        $("a.panel-tool-close").click(function (e) {
            if (window.confirm("确认取消编辑满减促销活动?")) {
                window.location.reload();
            }
        });
        var pro_class_value = $("[name='product_type_val']").val();
        if (pro_class_value == 3 || pro_class_value == 0) {
            $("#chooseShowListPanel").hide();
        }

        if (pro_class_value == 1) {
            $("#bf").prop("checked", true);
        }
        if (pro_class_value == "") {
            $("#bf").prop("checked", true);
        }

        $("#pro_class_set label input").each(function () {
            var pro_txt = $(this).attr("value");
            if (pro_class_value == pro_txt) {
                $(this).prop("checked", true);
            }
        });
        var manjian = "<w:write bind="bean.items"/>";
        if (manjian.length > 2) {
            $("#mj").prop("checked", true);
        }
        var manze = "<w:write bind="bean.itemsrate"/>"
        if (manze.length > 2) {
            $("#mz").prop("checked", true);
        }

    });


</script>
<script>
    function addoff_id() {
        $("p[name=discount_money]").each(function () {
            var id = $(this).attr("rmenu");
            var bb = $(this).attr("rmenu");
            var cc = $("p[rmenu=" + bb + "]");
            var dd = cc.find("input[type=text]");
            dd.each(function () {
                var inp = $(this).val();
//                console.log("-------" + inp);
                var aaaa = "{'" + id + "':'" + inp + "'}"
//                console.log(aaaa);
            })
        });
    }


    $(function () {
        var start = $("[name='bean.start_time']").val();
        $("[name='starttime']").val(start)
        var end = $("[name='bean.end_time']").val();
        $("[name='endtime']").val(end);
        chooseShowList();
        var aa = $("#pro2").find("p").attr("id");
//        console.log("*****" + aa);

    })

    function chooseShowList() {
        $w.execUrl(ctx + "/marketing/FillReductionPage@chooseMoneyList.page", {}, $('#chooseShowListPanel'));
    }

    /*信息不完整，创建活动失败*/
    function Createfailure() {
        $w.errorMsg("信息不完整，创建活动失败!");
    }
    /*活动创建成功*/
    function Createsuccess() {
        $("#errormsg").html("活动创建成功").fadeIn(300).delay(2000).fadeOut(300);
    }
    /*活动未创建成功，直接离开页面？*/
    function Notcreated() {
        window.parent.$w.confirmMsg("活动未创建成功，直接离开页面？", function () {

        })
    }
    /*点击完成按钮验证必填项*/
    $(function () {
        /*当前表单调用validate方法，实现表单验证功能*/
        $("#cutform").validate({
            debug: true, /*调试模式，验证通过也不跳转到目标页面，如要提交跳转修改成false*/
            rules: {
                /*配置验证规则*/
                platform: {
                    required: true/*必填。如果验证方法不需要参数，则配置为true*/
                },
                starttime: {
                    required: true
                },
                endtime: {
                    required: true
                },
                promotname: {
                    required: true
                },
                bean2: {
                    required: true
                },
                off_money_rat: {
                    required: true
                },
                discount_rate: {
                    required: true
                },
                off_money: {
                    required: true
                },
                discount_money: {
                    required: true
                },
            },
            messages: {
                platform: {
                    required: "推广平台不能为空，必选一项!"
                },
                starttime: {
                    required: "活动开始时间不能为空"
                },
                endtime: {
                    required: "活动结束时间不能为空"
                },
                promotname: {
                    required: "活动标题不能为空"
                },
                bean2: {
                    required: "活动规则不能为空，必选一项!"
                },
                off_money_rat: {
                    required: "满折金额不能为空"
                },
                discount_rate: {
                    required: "满折折扣不能为空"
                },
                off_money: {
                    required: "满减金额不能为空"
                },
                discount_money: {
                    required: "满减额度不能为空"
                },
            },
            /* 重写错误显示消息方法,以吐司方式弹出错误消息 */
            /*获取单项必填项消息*/
            showErrors: function (errorMap, errorList) {
                var msg = "";
                $.each(errorList, function (i, v) {
                    if (i == 0)//每次只弹出一个提示
                    {
                        msg += (v.message + "\r\n");
                    }
                });
                if (msg != "")
//                    toastr.warning(msg) || toastr.success(msg)
                    $("#errormsg").html(msg).fadeIn(300).delay(2000).fadeOut(300);
            },
            /* 失去焦点时不验证 */
            onfocusout: false,
            submitHandler: function (form) {
                pedit_prodoctupdate();
//                    addoff_id();
            }

        });
    });
    //删除控销用户
    function delcontroluser(obj) {
        $(obj).parent().parent().remove();
    }
    //添加控销
    function addControlsales() {
        window.parent.$w.openDialog({
            url: ctx + '/products/ProductPage@addControlsalesShow.page',
            title: "活动控销",
            w: 600,
            h: 400,
            afterClose: function (ret) {
                if (ret && ret.list.length > 0) {
                    var txt = "";
                    var combox = $("#controlsales").get(0);
                    for (var i = 0; i < ret.list.length; i++) {
                        if (ret.type == "1") txt = "客户分组：" + ret.list[i].text;
                        else if (ret.type == "2") txt = "价格等级：" + ret.list[i].text;
                        else if (ret.type == "3") txt = "区域控销：" + ret.list[i].text;
                        val = "{'type':'" + ret.type + "','val':'" + ret.list[i].val + "','text':'" + txt + "'}";
                        combox.options[combox.options.length] = new Option(txt, val);
                    }
                }
            }
        })
    }
    function delControlsales() {
        var combox = $("#controlsales").get(0);
        if (combox.options.length > 0) {
            for (var i = 0; i < combox.options.length; i++) {
                var option = combox.options[i];
                if (option.selected) {
                    combox.options[i] = null;
                    i--;
                }
            }
        }
    }
    /*规则限制单选框根据选中的值显示不同*/
    $(function () {
        showCont();
        $("input[name=bean2]").click(function () {
            showCont();
        });
    });
    function showCont() {
        switch ($("input[name=bean2]:checked").attr("id")) {
            case "mj":
                $(".folding").hide();
                $(".cut").show();
                break;
            case "mz":
                $(".cut").hide();
                $(".folding").show();
                break;
            default:
                break;
        }
    }
    var off_id;
    /*点击满减*/
    $("#cut_add").click(function () {
        $("p[name=money_tag]").each(function () {
            var arr = ( $(this).attr("rmenu"));
            off_id = arr;
        })
        off_id = parseInt(off_id) + 1;

        $("#cut").after("<p style='margin-left: 153px; '  rmenu='" + off_id + "' class='cut' name='money_tag'>" +
            "满<input name='off_money' value='' onblur='inputcheck(this);' maxlength='8' data-options='required:true' style='width:100px;margin: 0 5px;'>" + '&nbsp;' +
            "减<input name='discount_money' value='' onblur='inputcheck(this);' maxlength='8' data-options='required:true' style='width:100px;margin: 0 5px;'>" +
            "<a  href='javascipt:;' class='newbtna' onclick='del(this)'>&nbsp;[-]</a>" +
            "</p>");
    });
    /*点击满折*/
    $("#folding_add").click(function () {
        $("p[name=money_tag_rate]").each(function () {
            var arr = ( $(this).attr("rmenu"));
            off_id = arr;
        })
        off_id = parseInt(off_id) + 1;

        var foldingatt = $("#pro2").find("p").attr("id");
        $("#" + foldingatt).after("<p  style='margin-left: 153px; ' rmenu='" + off_id + "'  name='money_tag_rate' class='folding'>" +
            "满<input name='off_money_rat' value='' onblur='inputcheck(this);' maxlength='8' data-options='required:true' style='width:100px;margin: 0 5px;'>" + '&nbsp;' +
            "折扣<input name='discount_rate' value='' oninput='verify_rate(this);' onblur='verify_rate_to(this);' data-options='required:true' style='width:100px;margin: 0 5px;'  maxlength='2' >%" +
            "<a href='javascript:;' class='newbtna' onclick='del(this)'>&nbsp;[-]</a>" +
            "</p>");
    });
    /*点击-号删除*/
    function del(obj) {
        $(obj).parent().remove();
    }
    function inputcheck(obj) {
        obj.value = obj.value.trim().replace(/[^0-9|\.]/g, '');
        obj.value = obj.value.replace(/^\./, '0.');
        obj.value = obj.value.replace(/\.$/, '');
        if (obj.value.indexOf('.') > 0 && obj.value.indexOf('.') != (obj.value.length - 1)) {
            obj.value = obj.value.substring(0, obj.value.indexOf('.') + 1)
                + obj.value.substr(obj.value.indexOf('.') + 1, 2);
        }
        ;
        obj.value = obj.value.replace(/\./g, '_').replace('_', '.').replace(/_/g, '');
    }

    function verify_rate(obj) {
        obj.value = obj.value.trim().replace(/[^\d]/g, '')
    }
    function verify_rate_to(obj) {
        obj.value = obj.value.trim().replace(/[^\d]/g, '');
        if (obj.value.length == 1) {
            obj.value = '0' + obj.value;
        }
    }


    /*参与方式单选框根据选中的值显示不同*/
    $(function () {
        showactivity();
        $("input[name=bean4]").click(function () {
            showactivity();
        });
    });
    function showactivity() {
        switch ($("input[name=bean4]:checked").attr("id")) {
            case "qc":
                $("#puctfl").hide();
                $("#puctname").hide();
                $("#puctbar").hide();
                $("#puctbtn").hide();
                $("#seetab").hide();
                $("#seestab").hide();
                $("#selectedit").hide();
                break;
            case "bf":
                $("#puctfl").hide();
                $("#puctname").show();
                $("#puctbar").show();
                $("#puctbtn").show();
                $("#seetab").show();
                $("#selectedit").show();
                $("#chooseShowListPanel").show();
                break;
            case "zd":
                $("#puctfl").show();
                $("#puctname").hide();
                $("#puctbar").hide();
                $("#puctbtn").hide();
                $("#seetab").hide();
                $("#seestab").hide();
                $("#selectedit").hide();
                break;
            default:
                break;
        }
    }
    /*点击查看全部商品切换*/
    $.fn.toggle = function (fn) {
        var args = arguments,
            guid = fn.guid || jQuery.guid++,
            i = 0,
            toggler = function (event) {
                var lastToggle = ( jQuery._data(this, "lastToggle" + fn.guid) || 0 ) % i;
                jQuery._data(this, "lastToggle" + fn.guid, lastToggle + 1);
                event.preventDefault();
                return args[lastToggle].apply(this, arguments) || false;
            };
        toggler.guid = guid;
        while (i < args.length) {
            args[i++].guid = guid;
        }
        return this.click(toggler);
    }
    $(function () {
        $("#see").toggle(
            function () {
                $("#seetab").hide();
                $("#see").css("background-color", " #C9C9C9");
                $("#seestab").show();
            },
            function () {
                $("#seetab").show();
                $("#see").removeAttr("style")
                $("#seestab").hide();
            }
        )
    })
    /*点击查询*/
    $("#search").click(function () {
        $("#seetab").show();
        $("#seestab").hide();
        $("#see").removeAttr("style");
    })
    //实现日期选择联动
    var start = {
        format: 'YYYY-MM-DD hh:mm:ss',//日期格式
        minDate: $.nowDate({DD: 0}), //设定最小日期为当前日期
        maxDate: '2099-06-16 23:59:59', //最大日期
        festival: true,//是否显示节日
        isClaer: true,//是否显示清空
        isinitVal: false,//是否初始化时间
        zIndex: 300000,//层级高度
        choosefun: function (elem, datas) {
            end.minDate = datas; //开始日选好后，重置结束日的最小日期
            endDates();
        },//选中日期回调
        okfun: function (elem, datas) {
//            alert(datas)
        }//点击确定回调
    };
    var end = {
        format: 'YYYY-MM-DD hh:mm:ss',//日期格式
        minDate: $.nowDate({DD: 0}), //设定最小日期为当前日期
        maxDate: '2099-06-16 23:59:59', //最大日期
        festival: true,//是否显示节日
        isClaer: true,//是否显示清空
        isinitVal: false,//是否初始化时间
        zIndex: 300000,//层级高度
        choosefun: function (elem, datas) {
            start.maxDate = datas; //将结束日的初始值设定为开始日的最大日期
        }
    };
    //日期联动
    function endDates() {
        end.trigger = false; //结束日期的事件改成false
        $("#singEditinpend").jeDate(end);
    }
    $("#singEditInpstart").jeDate(start);
    $("#singEditinpend").jeDate(end);
</script>
<script>
    function query1() {
        $w.execForm({form: $('#cutform'), target: $('#chooseShowListPanel'), pageNum: 1})
    }
    function pedit_prodoctupdate() {
//        $.messager.progress();
        pedit_saveProduct();
    }
    var pedit_saveing = false;
    function pedit_saveProduct() {
        if ($("input[name='bean4']:checked").val() == 3) {
            if ($("[name='class_name']").val() == "" || $("[name='class_name']").val() == "null") {
                $("#errormsg").html("请选择要添加的类目").show(300).delay(3000).hide(300);
                return false;
            }
        }
        if (!pedit_saveing) {
            pedit_saveing = true;//防止重复保存
            var data = productdata();
            if (!data) {
                pedit_saveing = false;
                return false;
            }
            $w.doUrl(ctx + "/marketing/FillReductionPage@update.page", data, function (ret) {
                $.messager.progress('close');
                if (ret.state == 'ok') {
                    window.parent.$w.correctMsg("保存商品成功");
                    $w.closeCurrWin("ok");
                } else if (ret.state == 'no') {
                    pedit_saveing = false;
                    window.parent.$w.alertMsg("编辑失败,商品已参加活动");
                } else {
                    pedit_saveing = false;
                    window.parent.$w.alertMsg(ret.state);
                }
            })
        }
    }
    //组合数据
    function productdata() {
        var win = $w.currWin();
        var data = {
            "bean.promotid": $("[name='moneyid']").val(),
            "bean.start_time": $("#singEditInpstart").val(),
            "bean.name": $("[name='promotname']").val(),
            "bean.end_time": $("#singEditinpend").val(),
            "bean.description": $("[name='description']").val(),
            "bean.classid": $("[name='bean.classid1']").val(),
            "bean.time": $("[name='time']").val(),
            "bean.classname": $("[name='classname']").val(),
            "bean.sort_value": $("[name='sort_value']").val(),
            "bean.district_description": $("[name='district_description']").val()
        };
        data["bean.platform"] = $("input[name='platform']:checked").val();

        data["bean.product_type"] = $("input[name='bean4']:checked").val();
        data["bean.moneyoff_type"] = $("input[name='bean2']:checked").val();

        var productid_items = [];

        if (productItemsTo) {
            for (var key in productItemsTo) {
                var val = productItemsTo[key];
                console.log(val);
                if (val.showlistidto) {
                    productid_items.push(val.showlistidto);
                }
            }
        }

        data["bean.product_val"] = JSON.stringify(productid_items);


        var controlsales = "";//控销数据
        var combox = $("#controlsales").get(0);
        for (var i = 0; i < combox.options.length; i++) {
            if (controlsales != "") controlsales += ",";
            controlsales += combox.options[i].value;
        }
        data["bean.controlsales"] = "[" + controlsales + "]";
        var controlusers = "";//控销用户
        $("[name='controluserid']").each(function () {
            controlusers += this.value + ",";
        })
        data["bean.controlusers"] = controlusers
        var off_money_flag = true;
        var money_size_flag = true;
        var items = [];
        if ($("input[name='bean2']:checked").val() == 0) {
            $("[name='money_tag']").each(function () {
                if ($(this).find("[name='off_money']").val() == "" || $(this).find("[name='discount_money']").val() == "") {
                    off_money_flag = false;
                    return false;
                }
                if (($(this).find("[name='off_money']").val() - $(this).find("[name='discount_money']").val()) <= 0) {
                    money_size_flag = false;
                    return false;
                }
                var item = {};
                item["off_money"] = $(this).find("[name='off_money']").val();
                item["discount_money"] = $(this).find("[name='discount_money']").val();
                items.push(item);
            });
        }
        data["bean.bean2"] = $("input[name='bean2']:checked").val();
        if ($("input[name='bean2']:checked").val() == 1) {
            $("[name='money_tag_rate']").each(function () {
                if ($(this).find("[name='off_money_rat']").val() == "" || $(this).find("[name='discount_rate']").val() == "") {
                    off_money_flag = false;
                    return false;
                }
                var item = {};
                item["off_money"] = $(this).find("[name='off_money_rat']").val();
                item["discount_rate"] = $(this).find("[name='discount_rate']").val();
                items.push(item);
            });
        }

        if (!off_money_flag) {
            $("#errormsg").html("请填写完整的活动规则").show(300).delay(3000).hide(300);
            return false
        }
        if (!money_size_flag) {
            $("#errormsg").html("满金额不能小于或等于减金额").show(300).delay(3000).hide(300);
            return false
        }
        data["bean.money_off"] = JSON.stringify(items);

        return data;
    }
</script>
